﻿using System;
using System.Configuration;

using TempEmployees.DAL;
using TempEmployees.BLL;
using TempEmployees.BLL.Database;
using TempEmployees.BLL.UserAccess;
using Infrastructure.ServicesManager;
using TempEmployees.BLL.EntitiesExtensions;

namespace TempEmployees.Web
{
    public partial class WebFormLogin : ABasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            Page.Form.DefaultButton = ButtonLogin.UniqueID;
            
            SessionState state = (SessionState)Session[SessionState.SESSION_STATE];
            if (state != null && state.UserRole != DbConstants.Roles.None)
            {
                Response.Redirect(state.UserFirstPage);
            }

            LabelMessage.Text = String.Empty;
        }

        protected void ButtonLogin_Click(object sender, EventArgs e)
        {
            string username = TextBoxUsername.Text;
            string password = TextBoxPassword.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                LabelMessage.Text = "שם משתמש או סיסמא חסרים";
                return;
            }

            IUserDetails userDetails;
            try
            {
                ILoginService loginService = ServiceEngineSingleton.Instance.GetService<ILoginService>(ConfigurationManager.AppSettings["LoginService"].ToString());
                userDetails = loginService.Logon(username, password);
            }
            catch (LoginErrorException ex)
            {
                LabelMessage.Text = ex.Message;
                return;
            }

            TextBoxPassword.Visible = false;

            User user = CreateUser(userDetails);

            CreateSessionState(user);

            SetRoleSelection(user);
        }

        private User CreateUser(IUserDetails details)
        {
            User user = new User();
            user.ID = details.ID;
            user.FirstName = details.FirstName;
            user.LastName = details.LastName;
            user.Roles = details.Roles;
            EUser.UpdateUser(user, details.DepartmentID);
            return user;
        }

        private void CreateSessionState(User user)
        {
            if (user.GetUserRoles().Roles.Count == 1)
            {
                State = new SessionState(user.ID, (DbConstants.Roles)user.Roles, WebConfigConfigurations.Roles[user.Roles].FirstPage);
                Response.Redirect(State.UserFirstPage);
            }
            else
            {
                State = new SessionState(user.ID, DbConstants.Roles.None, string.Empty);
            }
        }

        private void SetRoleSelection(User user)
        {
            DbServices.SetDropDownRoles(DropDownListRoles, user);
            PanelLogonButton.Visible = false;
            PanelChooseRole.Visible = true;
        }

        protected void ButtonChooseRole_Click(object sender, EventArgs e)
        {
            if (DropDownListRoles.SelectedValue == "0") return;
            DbConstants.Roles role = (DbConstants.Roles)int.Parse(DropDownListRoles.SelectedValue);
            State = new SessionState(State.UserId, role, WebConfigConfigurations.Roles[(int)role].FirstPage);
            Response.Redirect(State.UserFirstPage);
        }
    }
}
